from django.contrib.auth import get_user_model
from django.shortcuts import render

from rest_framework import viewsets, authentication, permissions, filters

from .models import Sprint, Task
from .forms import TaskFilter, SprintFilter
from .serializers import SprintSerializer, TaskSerializer, UserSerializer
# Create your views here.

User = get_user_model()


class DefaultsMixin(object):
    """Default settings for view authentication, permissions, filtering and pagination. """
    authentication_classes = (
        authentication.BasicAuthentication,
        authentication.TokenAuthentication
    )
    permission_classes = (
        permissions.IsAuthenticated,
    )
    paginate_by = 25
    paginate_by_param = 'page_size'
    max_paginate_by = 100
    filter_backends = (
        #filters.DjangoFilterBackend,
        filters.SearchFilter,
        filters.OrderingFilter,
    )


class TaskViewSet(DefaultsMixin, viewsets.ModelViewSet):
    """Api endpoint for listing and creating tasks."""
    queryset = Task.objects.all()
    serializer_class = TaskSerializer
    #filter_class = TaskFilter
    search_fields = ('name', 'description',)
    ordering_fields = ('name', 'order', 'started_at', 'due_at', 'completed_at')


class UserViewSet(DefaultsMixin, viewsets.ModelViewSet):
    """Api endpoint for listing users."""
    lookup_field = User.USERNAME_FIELD
    lookup_url_kwarg = User.USERNAME_FIELD
    queryset = User.objects.order_by(User.USERNAME_FIELD)
    serializer_class = UserSerializer
    search_fields = (User.USERNAME_FIELD,)


class SprintViewSet(DefaultsMixin, viewsets.ModelViewSet):
    """Api endpoint for listing and creating sprints."""
    queryset = Sprint.objects.order_by('end_at')
    serializer_class = SprintSerializer
    #filter_class = SprintFilter
    search_fields = ('name',)
    ordering_fields = ('end_at', 'name',)
